<?php

namespace app\controller;

use app\BaseController;
use app\model\Category as CategoryModel;
use app\Request;
use app\service\Category as CategoryService;
use think\response\Json;

class Category extends BaseController
{
    private function model(): CategoryModel
    {
        return new CategoryModel();
    }
    public function index(): Json
    {
        $orgId = $this->organization;
        if(!$orgId){
            return apiResponse(400, '请选择组织');
        }
        try {
            $data = $this->model()->where('oid',$orgId)->select()->toArray();
            return apiResponse(200, '获取成功', $data);
        }catch (\Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 创建分类
     * @param Request $request
     * @return Json
     */
    public function save(Request $request): Json
    {
        try {
            $data = $request->param();
            $oid = $this->organization;
            //检查是否重名
            $isName = $this->model()
                ->where('oid',$oid)
                ->where('type', $data['type'])
                ->where('name', $data['name'])->find();

            if ($isName) {
                return apiResponse(400, '分类名称重复，请换一个重试');
            }
            $newData = [
                'oid' => $oid,
                'name' => $data['name'],
                'model' => $data['model'],
                'pid' => $data['pid'],
                'status' => $data['status']
            ];
            $this->model()->save($newData);
            return apiResponse(200, '分类添加成功');
        }catch (\Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }

    }
    public function update(Request $request): Json
    {
        try {
            $data = $request->param();
            //检查是否重名
            $isName = $this->model()
                ->where('oid',$data['oid'])
                ->where('name', $data['name'])
                ->where('type', $data['type'])
                ->where('id', '<>', $data['id'])->find();
            if ($isName) {
                return apiResponse(400, '分类名称重复，请换一个重试');
            }
            $this->model()->where('id',$data['id'])->update([
                'name' => $data['name'],
                'pid' => $data['pid'],
                'status' => $data['status']
            ]);
            return apiResponse(200, '分类修改成功');
        }catch (\Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
    public function batch_update(Request $request): Json
    {
        try {
            $data = $request->param();
            $this->model()->whereIn('id',$data['ids'])
                ->update($data['data']);
            return apiResponse(200, '分类修改成功');
        }catch (\Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
    public function delete($id): Json
    {
        try {
            $this->model()->where('id',$id)->delete();
            return apiResponse(200, '分类删除成功');
        }catch (\Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
    public function batch_delete(Request $request): Json
    {
        try {
            $ids = $request->param('ids');
            $this->model()->whereIn('id',$ids)->delete();
            return apiResponse(200, '分类删除成功');
        }catch (\Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
}